home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / SyntheticImage.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  135 lines

  1. /*
  2.  * @(#)SyntheticImage.java    1.6 98/01/30
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * @author James Gosling
  20.  */
  21.  
  22. package com.sun.java.swing;
  23.  
  24. import java.awt.*;
  25. import java.awt.image.*;
  26.  
  27. /** A helper class to make computing synthetic images a little easier.
  28.  *  All you need to do is define a subclass that overrides computeRow
  29.  *  to compute a row of the image.  It is passed the y coordinate of the
  30.  *  row and an array into which to put the pixels in
  31.  *  <a href="http://java.sun.com/products/jdk/1.1/docs/api/java.awt.image.ColorModel.html#getRGBdefault()">
  32.  *  standard ARGB format</a>.
  33.  *  <p>Normal usage looks something like this:
  34.  *  <pre> Image i = new Image(new SyntheticImage(200, 100) {
  35.  *       protected void computeRow(int y, int[] row) {
  36.  *       for(int i = width; --i>=0; ) {
  37.  *           int grey = i*255/(width-1);
  38.  *           row[i] = (255<<24)|(grey<<16)|(grey<<8)|grey;
  39.  *       }
  40.  *       }
  41.  *   }
  42.  *  </pre>This creates a image 200 pixels wide and 100 pixels high
  43.  *  that is a horizontal grey ramp, going from black on the left to
  44.  *  white on the right.
  45.  *  <p>
  46.  *  If the image is to be a movie, override isStatic to return false,
  47.  *  <i>y</i> cycling back to 0 is computeRow's signal that the next
  48.  *  frame has started.  It is acceptable (expected?) for computeRow(0,r)
  49.  *  to pause until the appropriate time to start the next frame.
  50.  *
  51.  *  @version 1.6 01/30/98
  52.  *  @author James Gosling
  53.  */
  54. public abstract class SyntheticImage implements ImageProducer {
  55.     private SyntheticImageGenerator root;
  56.     protected int width=10, height=100;
  57.     static final ColorModel cm = ColorModel.getRGBdefault();
  58.     public static final int pixMask = 0xFF;
  59.     private Thread runner;
  60.     protected SyntheticImage() {    }
  61.     protected SyntheticImage(int w, int h) { width = w; height = h; }
  62.     protected void computeRow(int y, int[] row) {
  63.         int p = 255-255*y/(height-1);
  64.         p = (pixMask<<24)|(p<<16)|(p<<8)|p;
  65.         for (int i = row.length; --i>=0; ) row[i] = p;
  66.     }
  67.     public synchronized void addConsumer(ImageConsumer ic){
  68.         for (SyntheticImageGenerator ics = root; ics != null; ics = ics.next)
  69.             if (ics.ic == ic) return;
  70.         root = new SyntheticImageGenerator(ic, root, this);
  71.     }
  72.     public synchronized boolean isConsumer(ImageConsumer ic){
  73.         for (SyntheticImageGenerator ics = root; ics != null; ics = ics.next)
  74.             if (ics.ic == ic) return true;
  75.         return false;
  76.     }
  77.     public synchronized void removeConsumer(ImageConsumer ic) {
  78.         SyntheticImageGenerator prev = null;
  79.         for (SyntheticImageGenerator ics = root; ics != null; ics = ics.next)
  80.             if (ics.ic == ic) {
  81.                 ics.useful = false;
  82.                 if (prev!=null) prev.next = ics.next;
  83.                 else root = ics.next;
  84.                 return;
  85.             }
  86.     }
  87.     public synchronized void startProduction(ImageConsumer ic) {
  88.         addConsumer(ic);
  89.         for (SyntheticImageGenerator ics = root; ics != null; ics = ics.next)
  90.             if (ics.useful && !ics.isAlive())
  91.                 ics.start();
  92.     }
  93.     protected boolean isStatic() { return true; }
  94.     public void nextFrame(int param) {}//Override if !isStatic
  95.     public void requestTopDownLeftRightResend(ImageConsumer ic){}
  96. }
  97.  
  98. class SyntheticImageGenerator extends Thread {
  99.     ImageConsumer ic;
  100.     boolean useful;
  101.     SyntheticImageGenerator next;
  102.     SyntheticImage parent;
  103.     SyntheticImageGenerator(ImageConsumer ic, SyntheticImageGenerator next,
  104.         SyntheticImage parent) {
  105.         this.ic = ic;
  106.         this.next = next;
  107.         this.parent = parent;
  108.         useful = true;
  109.         setDaemon(true);
  110.     }
  111.     public void run() {
  112.         ImageConsumer ic = this.ic;
  113.         int w = parent.width;
  114.         int h = parent.height;
  115.         int hints = ic.SINGLEPASS|ic.COMPLETESCANLINES|ic.TOPDOWNLEFTRIGHT;
  116.         if (parent.isStatic()) hints |= ic.SINGLEFRAME;
  117.         ic.setHints(hints);
  118.         ic.setDimensions(w, h);
  119.         ic.setProperties(null);
  120.         ic.setColorModel(parent.cm);
  121.         if (useful) {
  122.             int[] row=new int[w];
  123.             Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  124.             do {
  125.                 for (int y = 0; y<h && useful; y++) {
  126.                     parent.computeRow(y,row);
  127.                     ic.setPixels(0, y, w, 1, parent.cm, row, 0, w);
  128.                 }
  129.                 ic.imageComplete(parent.isStatic() ? ic.STATICIMAGEDONE
  130.                                             : ic.SINGLEFRAMEDONE );
  131.             } while(!parent.isStatic() && useful);
  132.         }
  133.     }
  134. }
  135.